package com.engine.jucailinkq.attendance.workflow.service.impl;

import com.engine.jucailinkq.attendance.workflow.cmd.GetEliminationsEvctionListCmd;
import com.engine.jucailinkq.attendance.workflow.service.ProcessSubmitCheckService;
import com.engine.jucailinkq.common.util.CommonUtil;
import com.engine.jucailinkq.common.util.DateUtil;
import com.engine.jucailinkq.common.util.DbTools;
import com.engine.jucailinkq.common.util.Utils;
import com.engine.core.impl.Service;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
public class ProcessSubmitCheckServiceImpl extends Service implements ProcessSubmitCheckService {
    @Override
    public Map<String, Object> processSubmitCheck(Map<String, Object> params) {
        String userId = Util.null2String(params.get("userId"));
        String submitDates = Util.null2String(params.get("submitDate"));
       //String modeId = Util.null2String(params.get("modeId"));
        String modeId = Utils.getFormmodeIdMap().get("uf_jcl_kq_kqzqmc");
        Map<String,Object> resultMap = Maps.newHashMap();
        resultMap.put("result","0");

        Set<String> attendanceSetIdsSets = CommonUtil.getDataIds(userId,modeId,DateUtil.getCurrentDate());
        log.debug("attendanceSetIdsSets : {}",attendanceSetIdsSets);
        for (String submitDate:submitDates.split(",")){
            LocalDateTime localDateTime = DateUtil.getTime(submitDate);
            int year = localDateTime.getYear();
            int month = localDateTime.getMonth().getValue();
            String sql = "select id,departmentid,subcompanyid1 from hrmresource where id =?";
            Map<String,Object> departMentMap = DbTools.getSqlToMap(sql,userId);
            String deptid = Util.null2String(departMentMap.get("departmentid"));
            String pdeptids = "";
            try {
                pdeptids = new DepartmentComInfo().getAllParentDepartId(Util.null2String(departMentMap.get("departmentid")), pdeptids);
                pdeptids = deptid + pdeptids;
                log.debug("pdeptids : [{}]",pdeptids);
                sql = "select a.zt,a.ksrq,a.gzrq,a.gzsj,b.zt dtzt,b.tsbm,b.gzrq dtgzrq,b.gzsj dtgzsj from uf_jcl_kq_kqzq a left join uf_jcl_kq_kqzq_dt1 b on a.id=b.mainid where a.nd=? and a.mc=? and a.yf=?";
                if (attendanceSetIdsSets.toArray().length > 0){
                    List<Map<String,Object>> list = DbTools.getSqlToList(sql,year,attendanceSetIdsSets.toArray()[0],month);
                    for (Map<String,Object> dataMap : list){
                        //先看人员是否在此笔考勤周期的明细中，如果在，看明细记录上的开关账状态，如果状态为关账，直接返回false，否则看明细记录上的关账日期时间是否小于当前日期时间，如果小于，返回false;
                        String tsbm = Util.null2String(dataMap.get("tsbm"));
                        String dtzt = Util.null2String(dataMap.get("dtzt"));
                        resultMap.put("openDate",dataMap.get("ksrq"));
                        if (!"".equals(tsbm) && CommonUtil.ifContainStr(pdeptids,tsbm,",")){
                            //明细表有值
                            String gzrq = Util.null2String(dataMap.get("dtgzrq")) +" "+Util.null2String(dataMap.get("dtgzsj")) +":00";
                            resultMap.put("closeDate",gzrq);
                            if (dtzt.equals("1")){
                                //关账
                                resultMap.put("result","1");
                                resultMap.put("submitDate",submitDate);
                            }else {
                                if (DateUtil.getBetWeenMinutes(gzrq,submitDate,DateUtil.yyyyMMddHHmm) >0){
                                    resultMap.put("result","1");
                                    resultMap.put("submitDate",submitDate);
                                }
                            }
                        }
                    }
                    //不在特殊部门中时
                    if (resultMap.get("closeDate") == null){
                        for (Map<String,Object> dataMap : list){
                            String zt = Util.null2String(dataMap.get("zt"));
                            String gzrq = Util.null2String(dataMap.get("gzrq")) +" "+Util.null2String(dataMap.get("gzsj")) +":00";
                            resultMap.put("closeDate",gzrq);
                            if (zt.equals("1")){
                                //关账
                                resultMap.put("result","1");
                                resultMap.put("submitDate",submitDate);
                            }else {
                                if (DateUtil.getBetWeenMinutes(gzrq,submitDate,DateUtil.yyyyMMddHHmm) >0){
                                    resultMap.put("result","1");
                                    resultMap.put("submitDate",submitDate);
                                }
                            }
                        }
                    }

                }

            }catch (Exception e){
                log.error("catch error :{}",e);
            }
        }

        return resultMap;
    }

    @Override
    public Map<String, Object> getEliminationsEvctionList(Map<String, Object> params) {
        return commandExecutor.execute(new GetEliminationsEvctionListCmd(params));
    }
}
